package com.company.huisuo.biz.activity

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.lifecycleScope
import com.company.huisuo.base.BaseActivity
import com.company.huisuo.biz.bean.ArtistBean
import com.company.huisuo.biz.bean.GiftBean
import com.company.huisuo.biz.dialog.ArtistGiftDialog
import com.company.huisuo.biz.dialog.ArtistRewardDataDialog
import com.company.huisuo.biz.dialog.RewardMemberDialog
import com.company.huisuo.biz.dialog.RewardSuccessDialog
import com.company.huisuo.biz.dialog.ScanPayDialog
import com.company.huisuo.biz.dialog.ScanSuccessDialog
import com.company.huisuo.biz.dialog.SelectPayTypeDialog
import com.company.huisuo.databinding.ActivityArtistRewardBinding
import com.company.huisuo.ext.loadCircleImg
import com.company.huisuo.ext.loadImg
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 艺人打赏
 */
class ArtistRewardActivity : BaseActivity<ActivityArtistRewardBinding>() {

    private var dataDialog: BasePopupView? = null
    private var giftDialog: BasePopupView? = null
    private val mRewardSuccessDialog by lazy {
        XPopup.Builder(this).asCustom(RewardSuccessDialog(this))
    }
    private val mScanSuccessDialog by lazy {
        XPopup.Builder(this).asCustom(ScanSuccessDialog(this) {
            //打赏成功
            mRewardSuccessDialog.show()
        })
    }
    private val mScanPayDialog by lazy {
        XPopup.Builder(this).asCustom(ScanPayDialog(this) {
            //支付成功
            mScanSuccessDialog.show()
        })
    }
    private val mMemberDialog by lazy {
        XPopup.Builder(this).asCustom(RewardMemberDialog(this) {
            mRewardSuccessDialog.show()
        })
    }
    private val mPayTypeDialog by lazy {
        XPopup.Builder(this).asCustom(SelectPayTypeDialog(this, {
            //扫码
            mScanPayDialog.show()
        }, {
            //会员
            mMemberDialog.show()
        }))
    }

    companion object {
        fun start(activity: Activity, bean: ArtistBean?, gift: Boolean = false) {
            Intent(activity, ArtistRewardActivity::class.java).apply {
                putExtra("Gift", gift)
                putExtra("artist", bean)
                activity.startActivity(this)
            }
        }
    }

    override fun initView() {
        val bean = intent.getParcelableExtra<ArtistBean>("artist")

        mBinding.ivBack.setOnClickListener {
            finish()
        }

        giftDialog = XPopup.Builder(this).asCustom(ArtistGiftDialog(this) { gift ->
            // 可以在这里处理选中的礼物信息
            // 例如：显示礼物名称、价格等
             gift?.let {
                 // 打印礼物信息，实际项目中可以显示在UI上或发送到服务器
                 println("选中的礼物: ${it.giftName}, 价格: ${it.giftAmount}")
             }
//            println("选中的礼物: ${gift.giftName}, 价格: ${gift.giftAmount}")
          
            mPayTypeDialog.show()
        })

        dataDialog =
            XPopup.Builder(this).asCustom(ArtistRewardDataDialog(this@ArtistRewardActivity, bean) {
                giftDialog?.show()
                dataDialog?.dismiss()
            })

        lifecycleScope.launch {
            delay(500)

            if (!intent.getBooleanExtra("Gift", false)) {
                dataDialog?.show()
            } else {
                giftDialog?.show()
            }
        }

        mBinding.clLayout.setOnClickListener {
            if (giftDialog?.isShow == false && dataDialog?.isShow == false) {
                dataDialog?.show()
            }
        }


        bean?.also {
            mBinding.ivAvatar.loadCircleImg(it.avatar)
            mBinding.tvName.text = it.name
            mBinding.ivBackground.loadImg(it.background)
        }
    }

    override fun initData() {

    }
}